<template>
  <div>
    <!-- 注意：表单绑定数据应该是:model,不应该是v-model(踩坑纪念) ⛹-->
    <el-form :inline="true" ref="searchFrom" :model="searchMap" class="demo-form-inline">
      <el-form-item prop="cardNum">
        <el-input v-model="searchMap.cardNum" placeholder="会员卡号"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="searchMap.name" placeholder="会员名称"></el-input>
      </el-form-item>
      <el-form-item prop="payType">
        <el-select v-model="searchMap.payType" placeholder="支付类型">
          <el-option
            v-for="option in payTypeOptions"
            :key="option.type"
            :label="option.name"
            :value="option.type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="birthday">
        <el-date-picker
          value-format="yyyy-MM-dd"
          v-model="searchMap.birthday"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="add">新增</el-button>
        <el-button @click="resetForm('searchFrom')">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="list" style="width: 100%" border height="470px">
      <el-table-column align="center" type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"></el-table-column>
      <el-table-column prop="name" label="会员姓名" width="90"></el-table-column>
      <el-table-column prop="birthday" label="会员生日" width="120"></el-table-column>
      <el-table-column prop="phone" label="手机号码"></el-table-column>
      <el-table-column prop="integral" label="可用积分" width="100"></el-table-column>
      <el-table-column prop="money" label="开卡金额" width="100"></el-table-column>
      <el-table-column prop="payType" label="支付类型" width="120">
        <template slot-scope="scope">
          <!-- 使用过滤器 -->
          {{scope.row.payType | payTypeOptions}}
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址"></el-table-column>
      <el-table-column align="center" label="操作" width="160">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[size,20,30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals"
    ></el-pagination>
    <!-- 自定义的新增对话框 -->
    <el-dialog title="会员编辑" :visible.sync="dialogFormVisible" width="540px">
      <el-form
        ref="memform"
        :rules="rules"
        :model="memform"
        label-position="right"
        style="width:450px"
      >
        <el-form-item label="会员卡号" :label-width="formLabelWidth" prop="cardNum">
          <el-input v-model="memform.cardNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名" :label-width="formLabelWidth" prop="name">
          <el-input v-model="memform.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员生日" :label-width="formLabelWidth" prop="birthday">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="memform.birthday"
            type="date"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="memform.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开卡金额" :label-width="formLabelWidth" prop="money">
          <el-input v-model="memform.money" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="可用积分" :label-width="formLabelWidth" prop="integral">
          <el-input v-model="memform.integral" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="支付类型" :label-width="formLabelWidth" prop="payType">
          <el-select v-model="memform.payType" placeholder="请点击选择">
            <el-option
              v-for="option in payTypeOptions"
              :key="option.type"
              :label="option.name"
              :value="option.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址" :label-width="formLabelWidth" prop="address">
          <el-input
            v-model="memform.address"
            autocomplete="off"
            placeholder="请输入地址"
            type="textarea"
            :rows="2"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="memform.id===null?addDate('memform'):updateDate('memform')"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import memberApi from "@/api/member";

const payTypeOptions = [
  { type: "1", name: "现金" },
  { type: "2", name: "微信" },
  { type: "3", name: "支付宝" },
  { type: "4", name: "银行卡" }
];

export default {
  data() {
    return {
      list: [],
      page: 1,
      totals: 0,
      size: 10, //每页显示条数
      searchMap: { cardNum: "", name: "", payType: "", birthday: "" }, //搜索关键词
      payTypeOptions,
      dialogFormVisible: false,
      formLabelWidth: "90px",
      memform: {
        id: null,
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        money: "",
        integral: "",
        payType: "",
        address: ""
      },
      rules: {
        cardNum: [
          { required: true, message: "请输入会员卡号", trigger: "blur" }
        ],
        name: [{ required: true, message: "请输入会员姓名", trigger: "blur" }],
        payType: [
          { required: true, message: "请选择支付类型", trigger: "change" }
        ]
      }
    };
  },
  created() {
    // 初始化的时候，就开始获取数据
    this.search();
  },
  components: {},

  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.search();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.search();
    },
    // 获取会员数据
    getMemberList() {
      memberApi.getList().then(res => {
        const resq = res.data;
        if (resq.flag) {
          this.list = resq.data;
          // console.log(this.list);
        } else {
          console.log("获取数据失败");
        }
      });
    },
    // 查询每页会员数据
    search() {
      memberApi.search(this.page, this.size, this.searchMap).then(res => {
        const resp = res.data;
        console.log(resp);
        this.totals = resp.data.total;
        this.list = resp.data.rows;
      });
    },
    add() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["memform"].resetFields();
      });
    },
    handleEdit(id) {
      console.log("编辑", id);
      this.add();
      memberApi.getById(id).then(res => {
        const resp = res.data;
        if (resp.flag) {
          this.memform = resp.data;
        } else {
          console.log("查询失败");
        }
      });
    },
    updateDate(formName) {
      this.$refs[formName].validate(val => {
        if (val) {
          memberApi.upDate(this.memform).then(res => {
            const resp = res.data;
            console.log(resp);
            if (resp.flag) {
              this.search();
              this.dialogFormVisible = false;
              this.$message({
                message: "编辑会员成功",
                type: "success"
              });
            } else {
              console.log("编辑失败");
            }
          });
        } else {
          return false;
        }
      });
    },
    handleDelete(id) {
      console.log("删除", id);
      this.$confirm("是否删除该记录", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          memberApi.delete(id).then(res => {
            const resp = res.data;
            this.$message({
              type: resp.flag?"success":"error",
              message:resp.message
            });
            if (resp.flag) {
              this.search();
            }
          });
        })
        .catch(() => {});
    },
    // 表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    addDate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          memberApi.addMember(this.memform).then(res => {
            const resp = res.data;
            if (resp.flag) {
              // 新增会员成功
              this.$message({
                message: "新增会员成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.search();
            } else {
              //新增失败
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  },
  filters: {
    // 定义过滤器过滤支付类型
    payTypeOptions(type) {
      const payObj = payTypeOptions.find(obj => obj.type === type);
      // 非空返回类型
      return payObj ? payObj.name : null;
    }
  }
};
</script>

<style scoped>
</style>